package com.hyjiacan.apps.audionly.data.repository

import com.hyjiacan.apps.audionly.data.model.TaskInfo
import java.util.concurrent.ConcurrentHashMap

/**
 * TaskRepository接口的实现类
 * 注意：这是一个内存实现，仅用于示例。实际应用中应使用数据库或其他持久化存储。
 */
class TaskRepositoryImpl : TaskRepository {
    // 使用ConcurrentHashMap存储任务，确保线程安全
    private val tasks = ConcurrentHashMap<String, TaskInfo>()

    override suspend fun insertTask(taskInfo: TaskInfo): Boolean {
        if (tasks.containsKey(taskInfo.id)) {
            return false
        }
        tasks[taskInfo.id] = taskInfo
        return true
    }

    override suspend fun updateTask(taskInfo: TaskInfo): Boolean {
        if (!tasks.containsKey(taskInfo.id)) {
            return false
        }
        tasks[taskInfo.id] = taskInfo
        return true
    }

    override suspend fun getTaskById(taskId: String): TaskInfo? {
        return tasks[taskId]
    }

    override suspend fun getAllTasks(): List<TaskInfo> {
        return tasks.values.toList()
    }

    override suspend fun getTasksByStatus(status: TaskInfo.Status): List<TaskInfo> {
        return tasks.values.filter { it.status == status }
    }

    override suspend fun deleteTask(taskId: String): Boolean {
        return tasks.remove(taskId) != null
    }
}